<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Think\Page;

class DhgoodsController extends BaseController {


    /**
     * 商品列表
     */
    public function goodsList(){
//        $where['status']=1;
        $model = M('dh_goods');
        $count = $model->where($where)->count();
        $Page  = new Page($count,10);
        $show = $Page->show();
        $goodsList = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('goodsList',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /**
     * 切换状态
     */
    public function cut_status(){
        $data['id']=I('id');
        if(I('status')==1){
            $data['status']=0;
        }else{
            $data['status']=1;
        }
        $res=M('dh_goods')->save($data);
        if($res){
            $this->success('操作成功!',U('goodsList'));
        }else{
            $this->error('操作失败，请重试!',U('goodsList'));
        }

    }

    /**
     * 删除兑换商品
     */
    public function goodsDelete(){
        $coach_info =M('dh_goods')->where(array('id'=>I('id')))->find();
        $images =$coach_info['images'];
        $key= end(explode('/',$images));
        qiniu_del_images($key);//删除七牛云图片

        $id=M('dh_goods')->where(array('id'=>I('id')))->delete();
        if($id){
            $this->success('删除成功!',U('goodsList'));
        }else{
            $this->error('删除失败，请重试!',U('goodsList'));
        }
    }


    /**
     * 编辑/添加
     */
    public function add(){
        $id=I('id');
        if($id){//编辑
            $info =M('dh_goods')->where(array('id'=>$id))->find();
            $this->assign('info',$info);
        }
        $this->display();
    }

    /**
     * 编辑/添加写入
     */
    public function save(){
        $data=I();
        $data['status']=1;
        $data['addtime']=time();
        if($_FILES['file']['name']){
            //执行七牛图片上传
            $images_name=  $_FILES['file']['tmp_name'];
            $new_name =date('YmdHis').rand(100000,999999);
            $data['images'] = qiniu_upload_images($images_name,$new_name);
            if($data['id']){
                $coach_info =M('dh_goods')->where(array('id'=>$data['id']))->find();
                $images =$coach_info['images'];
                $key= end(explode('/',$images));
                qiniu_del_images($key);//删除七牛云图片
            }
        }
        if($data['id']){//编辑
            $res =M('dh_goods')->save($data);
        }else{//新增
            $res =M('dh_goods')->add($data);
        }
        if($res){
            $this->success('操作成功!',U('goodsList'));
        }else{
            $this->error('操作失败，请重试!',U('goodsList'));
        }

    }


    /**
     * 兑换商品提交
     */

    public function dhGoods(){
        if(IS_POST){
            $user_id =I('user_id');
            $dh_goods_id =I('dh_goods_id');
            if(!$user_id){
                $this->error('请选择用户。');
            }
            //总推荐人数
            $z_num =M('users')->where("first_leader=$user_id")->count();
            //已兑换数量
            $y_num =M('dh_goods_log')->where("user_id=$user_id")->sum('num');
            //剩余未兑换数量
            $w_num =$z_num-$y_num;
//            $w_num=100;
            $where['id']=$dh_goods_id;
            $dh_goods_info =M('dh_goods')->where($where)->find();
            if($w_num<$dh_goods_info['num']){
                $this->error('可兑换数量不足。');
            }else{
                $add_data['user_id']=$user_id;
                $add_data['dh_goods_id']=$dh_goods_id;
                $add_data['addtime']=time();
                $add_data['num']=$dh_goods_info['num'];
                $add_data['operator']=$_SESSION['user']['user_id'];
                $res =  M('dh_goods_log')->add($add_data);
                if($res){
                    $this->success('兑换成功,请客户拿走相应的兑换商品。',U('dhGoods'),7);
                }else{
                    $this->error('操作失败，请重试!',U('dhGoods'));
                }
            }
        }
        $dh_goods_list=M('dh_goods')->where('status=1')->select();
        $this->assign('lists',$dh_goods_list);
        $this->display();
    }
}